<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="考核名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input :disabled=formDisabled v-model="model.name" placeholder="请输入考核名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="考核结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="result">
              <a-input :disabled=formDisabled v-model="model.result" placeholder="请输入考核结果"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="考核期始" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginDate">
              <j-date :disabled=formDisabled placeholder="请选择考核期始" v-model="model.beginDate" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="考核期止" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <j-date :disabled=formDisabled placeholder="请选择考核期止" v-model="model.endDate" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="考核单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-input :disabled=formDisabled v-model="model.unit" placeholder="请输入考核单位"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="考核内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-input :disabled=formDisabled v-model="model.content" placeholder="请输入考核内容"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'
import { validateBlankValue } from '@/utils/util'

export default {
  name: 'ErpEmployeeAssessmentForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请输入考核名称!' },
          { validator: (rule, value, callback) => validateBlankValue('erp_employee_assessment', 'name', value, this.model.id, callback) },
        ],
        result: [
          { required: true, message: '请输入考核结果!' },
          { validator: (rule, value, callback) => validateBlankValue('erp_employee_assessment', 'result', value, this.model.id, callback) },
        ],
      },
      url: {
        add: '/api/erp/files/employee/erpEmployeeAssessment/add',
        edit: '/api/erp/files/employee/erpEmployeeAssessment/edit',
        queryById: '/api/erp/files/employee/erpEmployeeAssessment/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.$refs.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>
